package com.pkutech.carcasher.view;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pkutech.carcasher.R;

import java.util.ArrayList;
import java.util.List;


public class PopMenuAction extends Fragment implements OnClickListener {

    public static final int FROM_TOP    = 0;
    public static final int FROM_BOTTOM = 1;

	private static final String ARG_CANCEL_BUTTON_TITLE = "cancel_button_title";
	private static final String ARG_OTHER_BUTTON_TITLES = "other_button_titles";
	private static final String ARG_CANCELABLE_ONTOUCHOUTSIDE = "cancelable_ontouchoutside";
	private static final String ARG_TITLE = "ARG_TITLE";
	private static final int CANCEL_BUTTON_ID = 100;
	private static final int BG_VIEW_ID = 10;
	private static final int TRANSLATE_DURATION = 200;
	private static final int ALPHA_DURATION = 300;

	private boolean mDismissed = true;
	private ActionSheetListener mListener;
	private View mView;
	private LinearLayout mPanel;
	private ViewGroup mGroup;
	private View mBg;
	private Attributes mAttrs;
	private boolean isCancel = true;

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

    private int from;
    public View getContentView() {
        return contentView;
    }

    public void setContentView(View contentView) {
        this.contentView = contentView;
    }

    private View contentView;

	public void show(FragmentManager manager, String tag) {
		if (!mDismissed) {
			return;
		}
		mDismissed = false;
		FragmentTransaction ft = manager.beginTransaction();
		ft.add(this, tag);
		ft.addToBackStack(null);
		ft.commitAllowingStateLoss();
	}

	public void dismiss() {
		if (mDismissed) {
			return;
		}
		mDismissed = true;
		getFragmentManager().popBackStack();
//		FragmentTransaction ft = getFragmentManager().beginTransaction();
//		ft.remove(this);
//		ft.commit();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		InputMethodManager imm = (InputMethodManager) getActivity()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive()) {
			View focusView = getActivity().getCurrentFocus();
			if (focusView != null) {
				imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
			}
		}

		mAttrs = readAttribute();

		mView = createView();
		mGroup = (ViewGroup) getActivity().getWindow().getDecorView();

		createItems();

		mGroup.addView(mView);
		mBg.startAnimation(createAlphaInAnimation());
		mPanel.startAnimation(createTranslationInAnimation());
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	private Animation createTranslationInAnimation() {
		int type = TranslateAnimation.RELATIVE_TO_SELF;
        TranslateAnimation an ;
        if (from == FROM_TOP){
            an = new TranslateAnimation(type, 0, type, 0, type,
                    -1, type, 0);
        } else if (from == FROM_BOTTOM){
            an = new TranslateAnimation(type, 0, type, 0, type,
                    1, type, 0);
        } else {
            an = new TranslateAnimation(type, 0, type, 0, type,
                    -1, type, 0);
        }
		an.setDuration(TRANSLATE_DURATION);
		return an;
	}

	private Animation createAlphaInAnimation() {
		AlphaAnimation an = new AlphaAnimation(0, 1);
		an.setDuration(ALPHA_DURATION);
		return an;
	}

	private Animation createTranslationOutAnimation() {
		int type = TranslateAnimation.RELATIVE_TO_SELF;
		TranslateAnimation an;

        if (from == FROM_TOP){
            an = new TranslateAnimation(type, 0, type, 0, type,
                    0, type, -1);
        } else if (from == FROM_BOTTOM){
            an = new TranslateAnimation(type, 0, type, 0, type,
                    0, type, 1);
        } else {
            an = new TranslateAnimation(type, 0, type, 0, type,
                    0, type, -1);
        }

		an.setDuration(TRANSLATE_DURATION);
		an.setFillAfter(true);
		return an;
	}

	private Animation createAlphaOutAnimation() {
		AlphaAnimation an = new AlphaAnimation(1, 0);
		an.setDuration(ALPHA_DURATION);
		an.setFillAfter(true);
		return an;
	}

	private View createView() {

        // 获取状态栏高度
        Rect frame = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        FrameLayout parent = new FrameLayout(getActivity());
        if (from == FROM_TOP){
            parent.setPadding(0,statusBarHeight+getResources().getDimensionPixelSize(R.dimen.vertical_height_48),0,0);
        } else if (from == FROM_BOTTOM){
            parent.setPadding(0,0,0,0);
        }

		parent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		mBg = new View(getActivity());
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
		mBg.setLayoutParams(lp);
		mBg.setBackgroundColor(Color.argb(136, 0, 0, 0));
		mBg.setId(BG_VIEW_ID);
		mBg.setOnClickListener(this);

		mPanel = new LinearLayout(getActivity());
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.MATCH_PARENT,from == Gravity.TOP? LayoutParams.MATCH_PARENT: LayoutParams.WRAP_CONTENT);

        if (from == FROM_TOP){
            params.gravity = Gravity.TOP;
        } else if (from == FROM_BOTTOM){
            params.gravity = Gravity.BOTTOM;
        }

		mPanel.setLayoutParams(params);
		mPanel.setOrientation(LinearLayout.VERTICAL);

		parent.addView(mBg);
		parent.addView(mPanel);
		return parent;
	}
	
	public List<ResolveInfo> getShareApps(Context context) {	
		List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
		Intent intent = new Intent(Intent.ACTION_SEND, null);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setType("text/plain");
//		intent.setType("*/*");
		PackageManager pManager = context.getPackageManager();
		mApps = pManager.queryIntentActivities(intent, 
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		return mApps;
	}
	
	class AppInfo{
		
		String appPkgName;
		String appLauncherClassName;
		String appName;
		Drawable appIcon;

		public void setAppPkgName(String packageName) {
			// TODO Auto-generated method stub
			this.appPkgName = packageName;
		}

		public void setAppLauncherClassName(String name) {
			// TODO Auto-generated method stub
			this.appLauncherClassName = name;
		}

		public void setAppName(String string) {
			// TODO Auto-generated method stub
			this.appName = string;
		}

		public void setAppIcon(Drawable appIcon) {
			// TODO Auto-generated method stub
			this.appIcon = appIcon;
		}

		public String getAppPkgName() {
			return appPkgName;
		}

		public String getAppLauncherClassName() {
			return appLauncherClassName;
		}

		public String getAppName() {
			return appName;
		}

		public Drawable getAppIcon() {
			return appIcon;
		}
		
		
	}
	
	private List<AppInfo> getShareAppList() {	
		List<AppInfo> shareAppInfos = new ArrayList<AppInfo>();
		PackageManager packageManager = getActivity().getPackageManager();
		List<ResolveInfo> resolveInfos = getShareApps(getActivity());
		if (null == resolveInfos) {
			return null;
		} else {
			for (ResolveInfo resolveInfo : resolveInfos) {
				AppInfo appInfo = new AppInfo();
				appInfo.setAppPkgName(resolveInfo.activityInfo.packageName);
//				showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name);
				appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name);
				appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
				appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
				shareAppInfos.add(appInfo);
			}
		}		
		return shareAppInfos;
	}
	
	public class AppsAdapter extends BaseAdapter {
		LayoutInflater mLayoutInflater;
		List<AppInfo> mApps;
        public AppsAdapter(List<AppInfo> _mApps) {
        	mApps = _mApps;
        	mLayoutInflater = getLayoutInflater(null);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View  i;
            ImageView icon;
            TextView tv;
            if (convertView == null) {
            	i = mLayoutInflater.inflate(R.layout.adapter_share_app, parent, false);
                i.setLayoutParams(new GridView.LayoutParams(dp2px(100), dp2px(100)));
            } else {
                i = convertView;
            }

            AppInfo info = mApps.get(position);
            icon = (ImageView) i.findViewById(R.id.icon);
            tv = (TextView) i.findViewById(R.id.text);
            icon.setImageDrawable(info.getAppIcon());
            tv.setText(info.getAppName());
            
            return i;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

	private void createItems() {

        if(null != contentView)
		    mPanel.addView(contentView);
		mPanel.setBackgroundColor(getResources().getColor(R.color.pop_menu_bg));
		mPanel.setPadding(0, 0, 0,0);
	}

	private int dp2px(int dp){
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				dp, getResources().getDisplayMetrics());
	}
	public LinearLayout.LayoutParams createButtonLayoutParams() {
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		return params;
	}

	private Drawable getOtherButtonBg(String[] titles, int i) {
		if (titles.length == 1) {
			return mAttrs.otherButtonSingleBackground;
		}
		if (titles.length == 2) {
			switch (i) {
			case 0:
				return mAttrs.otherButtonTopBackground;
			case 1:
				return mAttrs.otherButtonBottomBackground;
			}
		}
		if (titles.length > 2) {
			if (i == 0) {
				return mAttrs.otherButtonTopBackground;
			}
			if (i == (titles.length - 1)) {
				return mAttrs.otherButtonBottomBackground;
			}
			return mAttrs.getOtherButtonMiddleBackground();
		}
		return null;
	}

	@Override
	public void onDestroyView() {
		mPanel.startAnimation(createTranslationOutAnimation());
		mBg.startAnimation(createAlphaOutAnimation());
		mView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mGroup.removeView(mView);
			}
		}, ALPHA_DURATION);
		if (mListener != null) {
			mListener.onDismiss(this, isCancel);
		}
		super.onDestroyView();
	}

	private Attributes readAttribute() {
		Attributes attrs = new Attributes(getActivity());
//		TypedArray a = getActivity().getTheme().obtainStyledAttributes(null,
//				R.styleable.ShareAction, R.attr.actionSheetStyle, 0);
//		Drawable background = a
//				.getDrawable(R.styleable.ActionSheet_actionSheetBackground);
//		if (background != null) {
//			attrs.background = background;
//		}
//		Drawable cancelButtonBackground = a
//				.getDrawable(R.styleable.ActionSheet_cancelButtonBackground);
//		if (cancelButtonBackground != null) {
//			attrs.cancelButtonBackground = cancelButtonBackground;
//		}
//		Drawable otherButtonTopBackground = a
//				.getDrawable(R.styleable.ActionSheet_otherButtonTopBackground);
//		if (otherButtonTopBackground != null) {
//			attrs.otherButtonTopBackground = otherButtonTopBackground;
//		}
//		Drawable otherButtonMiddleBackground = a
//				.getDrawable(R.styleable.ActionSheet_otherButtonMiddleBackground);
//		if (otherButtonMiddleBackground != null) {
//			attrs.otherButtonMiddleBackground = otherButtonMiddleBackground;
//		}
//		Drawable otherButtonBottomBackground = a
//				.getDrawable(R.styleable.ActionSheet_otherButtonBottomBackground);
//		if (otherButtonBottomBackground != null) {
//			attrs.otherButtonBottomBackground = otherButtonBottomBackground;
//		}
//		Drawable otherButtonSingleBackground = a
//				.getDrawable(R.styleable.ActionSheet_otherButtonSingleBackground);
//		if (otherButtonSingleBackground != null) {
//			attrs.otherButtonSingleBackground = otherButtonSingleBackground;
//		}
//		attrs.cancelButtonTextColor = a.getColor(
//				R.styleable.ActionSheet_cancelButtonTextColor,
//				attrs.cancelButtonTextColor);
//		attrs.otherButtonTextColor = a.getColor(
//				R.styleable.ActionSheet_otherButtonTextColor,
//				attrs.otherButtonTextColor);
//		attrs.padding = (int) a.getDimension(
//				R.styleable.ActionSheet_actionSheetPadding, attrs.padding);
//		attrs.otherButtonSpacing = (int) a.getDimension(
//				R.styleable.ActionSheet_otherButtonSpacing,
//				attrs.otherButtonSpacing);
//		attrs.cancelButtonMarginTop = (int) a.getDimension(
//				R.styleable.ActionSheet_cancelButtonMarginTop,
//				attrs.cancelButtonMarginTop);
//		attrs.actionSheetTextSize = a.getDimensionPixelSize(R.styleable.ActionSheet_actionSheetTextSize, (int) attrs.actionSheetTextSize);

//		a.recycle();
		return attrs;
	}

	private String getCancelButtonTitle() {
		return getArguments().getString(ARG_CANCEL_BUTTON_TITLE);
	}

	private String[] getOtherButtonTitles() {
		return getArguments().getStringArray(ARG_OTHER_BUTTON_TITLES);
	}

	private boolean getCancelableOnTouchOutside() {
		return getArguments().getBoolean(ARG_CANCELABLE_ONTOUCHOUTSIDE);
	}

	public void setActionSheetListener(ActionSheetListener listener) {
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == BG_VIEW_ID && !getCancelableOnTouchOutside()) {
			return;
		}
		dismiss();
		if (v.getId() != CANCEL_BUTTON_ID && v.getId() != BG_VIEW_ID) {
			if (mListener != null) {
				mListener.onOtherButtonClick(this, v.getId() - CANCEL_BUTTON_ID
						- 1);
			}
			isCancel = false;
		}
	}

	public static Builder createBuilder(Context context,
			FragmentManager fragmentManager) {
		return new Builder(context, fragmentManager);
	}

	private static class Attributes {
		private Context mContext;

		public Attributes(Context context) {
			mContext = context;
			this.background = new ColorDrawable(Color.TRANSPARENT);
			this.cancelButtonBackground = new ColorDrawable(Color.BLACK);
			ColorDrawable gray = new ColorDrawable(Color.GRAY);
			this.otherButtonTopBackground = gray;
			this.otherButtonMiddleBackground = gray;
			this.otherButtonBottomBackground = gray;
			this.otherButtonSingleBackground = gray;
			this.cancelButtonTextColor = Color.WHITE;
			this.otherButtonTextColor = Color.BLACK;
			this.padding = dp2px(20);
			this.otherButtonSpacing = dp2px(2);
			this.cancelButtonMarginTop = dp2px(10);
			this.actionSheetTextSize = dp2px(16);
		}
		
		private int dp2px(int dp){
			return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
					dp, mContext.getResources().getDisplayMetrics());
		}

		public Drawable getOtherButtonMiddleBackground() {
//			if (otherButtonMiddleBackground instanceof StateListDrawable) {
//				TypedArray a = mContext.getTheme().obtainStyledAttributes(null,
//						R.ShareAction.ActionSheet, R.attr.actionSheetStyle, 0);
//				otherButtonMiddleBackground = a
//						.getDrawable(R.styleable.ActionSheet_otherButtonMiddleBackground);
//				a.recycle();
//			}
			return otherButtonMiddleBackground;
		}

		Drawable background;
		Drawable cancelButtonBackground;
		Drawable otherButtonTopBackground;
		Drawable otherButtonMiddleBackground;
		Drawable otherButtonBottomBackground;
		Drawable otherButtonSingleBackground;
		int cancelButtonTextColor;
		int otherButtonTextColor;
		int padding;
		int otherButtonSpacing;
		int cancelButtonMarginTop;
		float actionSheetTextSize;
	}

	public static class Builder {

		private Context mContext;
		private FragmentManager mFragmentManager;
		private String mCancelButtonTitle;
		private String title;
		private String[] mOtherButtonTitles;
		private String mTag = "actionSheet";
		private boolean mCancelableOnTouchOutside;
		private ActionSheetListener mListener;
        private View contentView;
        private int from;
		public Builder(Context context, FragmentManager fragmentManager) {
			mContext = context;
			mFragmentManager = fragmentManager;
		}

		public Builder setCancelButtonTitle(String title) {
			mCancelButtonTitle = title;
			return this;
		}
		
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setCancelButtonTitle(int strId) {
			return setCancelButtonTitle(mContext.getString(strId));
		}

		public Builder setOtherButtonTitles(String... titles) {
			mOtherButtonTitles = titles;
			return this;
		}

		public Builder setTag(String tag) {
			mTag = tag;
			return this;
		}

		public Builder setListener(ActionSheetListener listener) {
			this.mListener = listener;
			return this;
		}

		public Builder setCancelableOnTouchOutside(boolean cancelable) {
			mCancelableOnTouchOutside = cancelable;
			return this;
		}

        public Builder setContentView(View view) {
            contentView = view;
            return this;
        }

        public Builder setFrom(int view) {
            from = view;
            return this;
        }

		public Bundle prepareArguments() {
			Bundle bundle = new Bundle();
			bundle.putString(ARG_CANCEL_BUTTON_TITLE, mCancelButtonTitle);
			bundle.putStringArray(ARG_OTHER_BUTTON_TITLES, mOtherButtonTitles);
			bundle.putBoolean(ARG_CANCELABLE_ONTOUCHOUTSIDE,
					mCancelableOnTouchOutside);
			bundle.putString(ARG_TITLE,
					this.title);
			return bundle;
		}

		public PopMenuAction show() {
			PopMenuAction actionSheet = (PopMenuAction) Fragment.instantiate(
					mContext, PopMenuAction.class.getName(), prepareArguments());
            actionSheet.setContentView(contentView);
			actionSheet.setActionSheetListener(mListener);
			actionSheet.show(mFragmentManager, mTag);
            actionSheet.setFrom(from);
			return actionSheet;
		}

	}

	public static interface ActionSheetListener {

		void onDismiss(PopMenuAction actionSheet, boolean isCancel);

		void onOtherButtonClick(PopMenuAction actionSheet, int index);
	}

}